/**
*  \file can.h
*
*  \brief Can Abstraction Interface
*
*  \author Michael J. Hohmann  michael.hohmann@linde-mh.de
*
*/
#ifndef CAN_H_INCLUDED
#define CAN_H_INCLUDED

/*================================================[ public includes  ]================================================*/

#include <xmc4500/can_drv.h>
#include "../cfg/prj.h"
#include "../canid/canid.h"
#include "../candispatcher/candispatcher.h"

/*================================================[ public defines   ]================================================*/

/** Type of a callback function  */
typedef candispatcher_callback_function_t can_callback_function_t;

/*================================================[ public datatypes ]================================================*/

/** CAN Channels */
enum can_channel
{
   can_channel_int      = CAN_PORT_INT, /* communication with power mcu */
   can_channel_ext      = CAN_PORT_EXT, /* communication with external ecu, like debug tool */
   can_channle_max
};

/*================================================[ public variables ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/
/**
*  Initialization.
*
*  Initialize the can module.
*
*  \return RC_SUCCESS if successful.
*/
RC can_initialize( void );

/** Install can receive callback
*
*	\param channel 	Index of the CAN-BUS
*	\param id	   	Identifier to receive.
*	\param callback 	Function that should be called if message with id is received.
*	\param data		Data pointer that will be passed to the callback function.
*
*/
void can_install_rx_callback( enum can_channel channel, canmsg_id_t id, can_callback_function_t callback, void* data );

/** Send a CAN-Message
*
*	\param channel 	Index of the CAN-BUS
*	\param msg Pointer to the CAN-Message that should be send
*
*/
void can_tx_msg(enum can_channel channel, struct canmsg *msg);

void can_tx( enum can_channel channel, U32 id, void const* data, U8 len );


/** Process a received CAN-Message
*
* 	It is expected that this function is called from interrupt context where the message was received.
*
*	\param channel 	Index of the CAN-BUS
*	\param msg Pointer to a CAN-Message that was received.
*
*/
void can_process_message(struct canmsg *msg);

/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2018 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/
